package com.rocching.easychat.util;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.FileUtils;
import android.util.Log;

import androidx.room.util.FileUtil;

import com.rocching.easychat.R;
import com.rocching.easychat.core.UserIdentityContext;
import com.rocching.easychat.model.AppConst;
import com.rocching.easychat.model.FriendModel;
import com.rocching.easychat.model.UserModel;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.UUID;

import io.rong.imlib.model.UserInfo;

public class AppUtil {
    private final static String TAG = AppUtil.class.getName();

    public static String getGroupId(long id) {
        return String.format("G_%s", id);
    }

    public static long getGroupId(String groupId) {
        if (StringUtil.isEmpty(groupId)) {
            return 0L;
        }
        String newId = groupId.replace("G_", "");
        return Long.parseLong(newId);
    }

    public static String getTargetUserId(long id) {
        return String.format("U_%s", id);
    }

    public static String getTargetUserId(FriendModel data) {
        if (data.getId() > 0) {
            return getTargetUserId(data.getId());
        }
        return data.getAccount();
    }

    public static UserInfo getUserInfo(FriendModel data) {
        Uri uri = null;
        if (StringUtil.isNotEmpty(data.getPhoto())) {
            uri = Uri.parse(getUserPhoto(data.getPhoto()));
        }
        Log.i(TAG, "the uri is:" + (uri == null ? "null" : uri.toString()));
        UserInfo info = new UserInfo(getTargetUserId(data), data.getName(), uri);
        info.setAlias(data.getRemark());
        return info;
    }

    public static UserInfo getUserInfo(UserModel data) {
        Uri uri = null;
        if (StringUtil.isNotEmpty(data.getPhoto())) {
            uri = Uri.parse(String.format("%s%s", AppConst.API_URL, data.getPhoto()));
        }
        Log.i(TAG, "the uri is:" + (uri == null ? "null" : uri.toString()));
        return new UserInfo(getTargetUserId(data.getId()), data.getName(), uri);
    }

    public static String getUserPhoto(String photo) {
        if (StringUtil.isEmpty(photo)) {
            return AppConst.CHAT_USER_PHOTO_URL;
        }
        return String.format("%s%s", AppConst.API_URL, photo);
    }

    public static Uri getUserPhotoUri(String photo) {
        return getUri(getUserPhoto(photo));
    }

    public static Uri getGroupPhotoUri(String photo) {
        return getUri(getUserPhoto(photo));
    }

    public static Uri getSystemPhoto() {
        return getUri(AppConst.CHAT_SYSTEM_PHOTO_URL);
    }

    private static Uri getUri(String url) {
        try {
            return Uri.parse(url);
        } catch (Exception e) {
            return null;
        }
    }

    public static String getStatusName(Context context, Integer status, long id) {
        int resId = 0;
        switch (status) {
            case -1:
                resId = R.string.friend_status__1;
                break;
            case 0:
                resId = R.string.friend_status_0;
                break;
            case 1:
                resId = R.string.friend_status_1;
                break;
            case 50:
                resId = R.string.friend_status_50;
                break;
            case 100:
                resId = R.string.friend_status_100;
                break;
        }
        if (id == UserIdentityContext.getUserId()) {
            resId = R.string.friend_status_none;
        }
        return context.getString(resId);
    }

    public static Drawable getImageDrawable(String url) {
        try {
            Log.i(TAG, "the url is:" + url);
            String srcName = UUID.randomUUID().toString();
            InputStream is = (InputStream) new URL(url).getContent();
            return Drawable.createFromStream(is, srcName);
        } catch (Exception e) {
            Log.e(TAG, e.getMessage(), e);
            return null;
        }
    }

    public static Bitmap getBitmapFromURL(String src) {
        try {
            Log.e("src", src);
            URL url = new URL(src);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setDoInput(true);
            connection.connect();
            InputStream input = connection.getInputStream();
            Bitmap myBitmap = BitmapFactory.decodeStream(input);
            Log.e("Bitmap", "returned");
            return myBitmap;
        } catch (Exception e) {
            e.printStackTrace();
            Log.e("Exception", e.getMessage());
            return null;
        }
    }

    public static byte[] bitmapToBytes(Bitmap bitmap) {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream);
        return outputStream.toByteArray();
    }
}
